package com.aaa.sys.controller;



import com.aaa.sys.entity.Dept;
import com.aaa.sys.service.DeptService;
import com.aaa.vo.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 部门表 前端控制器
 * </p>
 *
 * @author ykq
 * @since 2020-11-26
 */
@RestController
@RequestMapping("/system/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping("findDeptTree")
    public R findDeptTree(){
        List<Dept> deptTree=deptService.findDeptTree();
        return R.ok().data("deptTree",deptTree);
    }
    @PostMapping("searchDept/{page}/{limit}")
    public R searchDept(@PathVariable Integer page, @PathVariable Integer limit, @RequestBody Dept dept){
        Page<Dept> deptPage = this.deptService.searchDept(page, limit, dept);
        return R.ok().data("records",deptPage.getRecords()).data("total",deptPage.getTotal());
    }
    @PostMapping("addDept")
    public R addDept(@RequestBody Dept dept){
        try {
            boolean b = this.deptService.saveOrUpdate(dept);
            return R.ok().message("添加部门成功");
        }catch (Exception e){
            return R.error().message("添加部门失败");
        }
    }

    @PostMapping("updateDept")
    public R updateDept(@RequestBody Dept dept){
        try {
            boolean b = this.deptService.updateById(dept);
            return R.ok().message("添加部门成功");
        }catch (Exception e){
            return R.error().message("添加部门失败");
        }
    }

    @PostMapping("deleteDept/{id}")
    public R deleteDept(@PathVariable Long id){
        try {
            boolean b = this.deptService.removeById(id);
            return R.ok().message("删除部门成功");
        }catch (Exception e){
            return R.error().message("删除部门失败");
        }
    }
    @GetMapping("findById/{id}")
    public R findById(@PathVariable Long id){
        try {
            Dept dept = this.deptService.getById(id);

            return R.ok().data("dept",dept);
        }catch (Exception e){
            return R.error();
        }
    }
}

